package com.mit.web.business;

import java.util.Date;
import java.util.List;

import com.mit.common.RequestContext;
import com.mit.common.ResultContext;
import com.mit.common.exception.BaseException;
import com.mit.common.inventory.dto.MaterialReceivedNoteDTO;
import com.mit.common.inventory.dto.MaterialReceivedNoteItemDTO;
import com.mit.common.inventory.dto.WarehouseDTO;
import com.mit.common.security.dto.UserDTO;
import com.mit.common.token.InventoryTokens;
import com.mit.common.token.Tokens;

public class InventoryService extends RemoteService {

	private final String FACADE_NAME = "InventoryFacade";
	@Override
	public String getFacadeName() {
		return FACADE_NAME;
	}
	
	public List<WarehouseDTO> retrieveWarehouses(UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("retrieveWarehouses", currentUser);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);		
		List<WarehouseDTO> lst = (List<WarehouseDTO>) rlt.getResult(Tokens.KEY_LIST);
		return lst;
	}
	
	public List<WarehouseDTO> saveWarehouses( List<WarehouseDTO> warehouses, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("saveWarehouses", currentUser);
		rqs.addParameter(Tokens.KEY_LIST, warehouses);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);		
		List<WarehouseDTO> lst = (List<WarehouseDTO>) rlt.getResult(Tokens.KEY_LIST);
		return lst;
	}
	
	public List<WarehouseDTO> retrieveWarehouseBasicInfo(  UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("retrieveWarehouseBasicInfo", currentUser);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);		
		List<WarehouseDTO> lst = (List<WarehouseDTO>) rlt.getResult(Tokens.KEY_LIST);
		return lst;
	}
	
	public List<MaterialReceivedNoteDTO> retrievePagedMaterialReceivedNotes(String poCode, String code, Integer supplierId, Date dateFrom, 
			Date dateTo, String state, Integer pageNo, Integer pageSize, UserDTO currentUser)throws BaseException{
		RequestContext rqs = createRequestContext("retrievePagedMaterialReceivedNotes", currentUser);
		rqs.addParameter(InventoryTokens.KEY_PO_CODE, poCode);
		rqs.addParameter(Tokens.KEY_CODE, code);
		if(supplierId > 0)
			rqs.addParameter(InventoryTokens.KEY_SUPPLIER_ID, supplierId);
		rqs.addParameter(Tokens.KEY_DATE_FROM, dateFrom);
		rqs.addParameter(Tokens.KEY_DATE_TO, dateTo);
		rqs.addParameter(Tokens.KEY_PAGE_NO, pageNo);
		rqs.addParameter(Tokens.KEY_PAGE_SIZE, pageSize);
		rqs.addParameter(Tokens.KEY_STATE, state);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);	
		List<MaterialReceivedNoteDTO> lst = (List<MaterialReceivedNoteDTO>) rlt.getResult(Tokens.KEY_LIST);
		return lst;
	}
	
	public Integer retrieveMaterialReceivedNoteCount(String poCode, String code, Integer supplierId, Date dateFrom, 
			Date dateTo, String state, UserDTO currentUser)throws BaseException{
		RequestContext rqs = createRequestContext("retrieveMaterialReceivedNoteCount", currentUser);
		rqs.addParameter(InventoryTokens.KEY_PO_CODE, poCode);
		rqs.addParameter(Tokens.KEY_CODE, code);
		if(supplierId > 0)
			rqs.addParameter(InventoryTokens.KEY_SUPPLIER_ID, supplierId);
		rqs.addParameter(Tokens.KEY_DATE_FROM, dateFrom);
		rqs.addParameter(Tokens.KEY_DATE_TO, dateTo);
		rqs.addParameter(Tokens.KEY_STATE, state);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);	
		Integer rtn = (Integer) rlt.getResult(Tokens.KEY_RECORD_COUNT);
		return rtn;
	}

	public List<MaterialReceivedNoteItemDTO> retrievePendingReceiveMaterial(Integer supplierId, UserDTO currentUser)throws BaseException{
		RequestContext rqs = createRequestContext("retrievePendingReceiveMaterial", currentUser);
		rqs.addParameter(InventoryTokens.KEY_SUPPLIER_ID, supplierId);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);	
		return (List<MaterialReceivedNoteItemDTO>) rlt.getResult(Tokens.KEY_LIST);
	}
}
